/* "Efficiency and water use: Dynamic effects of irrigation technology adoption"
by Micah Cameron-Harp and Nathan Hendricks

Code written by Micah Cameron-Harp
May 16th, 2024

This do file graphs the Callaway and Sant'Anna estimator dynamic treatment effect 
	results for both transitions during the period when the pre-test passes
	(1996-2005). The estimates are produced by an earlier line of code in 
	the "appendices_results.do" file.
*/

/* NOTE - Global macros for directories are created in the main_text_results.do 
	file which calls this individual .do file */

/***************************** Flood to CP/LEPA *******************************/
/* Import the flood to cp or lepa event study coefficients */
	import excel "${dr_temp}/cs_floodtocporlepa_96_05_nyt.xlsx", firstrow sheet(cs_event) clear
	gen estimator = "C&S"
	foreach var of varlist effect_estimated estimate se_estimate c_crit_val panel_mean_dep_var {
		destring `var', replace
	}
	gen scaled_estimate = 100*estimate/panel_mean_dep_var
	rename (ci_95_lb ci_95_ub) (lb_estimate ub_estimate)
	gen scaled_lb = 100*lb_estimate/panel_mean_dep_var
	gen scaled_ub = 100*ub_estimate/panel_mean_dep_var
	keep dep_var estimator effect_estimated scaled_estimate scaled_lb scaled_ub estimate lb_estimate ub_estimate
		
	*Generate column indicating tech transition
	gen transition = "flood_to_cp"
 
*Sort 
sort dep_var effect_estimated

*Replace extreme ub and lb estimates with -50 and +50 to make graphs readable 
gen extreme = scaled_ub > 40 | scaled_lb < -40
replace scaled_ub = 40 if scaled_ub > 40
replace scaled_lb = -40 if scaled_lb < -40


/* Graph C&S, C&D, and TWFE together */
*AF_USED_IRR
tw	(rcap scaled_ub scaled_lb effect_estimated if estimator=="C&S" & dep_var=="af_used" & extreme!=1,  msize(.2) lwidth(.1) lcolor(black%70)) /// 
	(line scaled_estimate effect_estimated if estimator=="C&S" & dep_var=="af_used", lwidth(.1) lcolor(maroon%70)) ///
	(scatter scaled_estimate effect_estimated if estimator=="C&S" & dep_var=="af_used", msize(.2) mcolor(maroon%70) msymbol(O)) /// 
	(rcap scaled_ub scaled_lb effect_estimated if extreme==1 & dep_var=="af_used",  msize(0) lpattern("shortdash") lwidth(.1) lcolor(black%70)), /// 
			title("        Flood to center pivot (1996 to 2005)", size(small)) ///
			subtitle("Acre-feet withdrawn", size(small)) ///
			xtitle("Time relative to adoption (years)", size(vsmall)) ///
			yline(0, lpattern(solid)) ///
			ytitle("Percent change", size(small)) ///
			ylab(-40(20)40, ang(45) nogrid) ///
			xlab(-8(2)8, nogrid) ///
			legend(order(2) label(2 "{it:{&delta}{superscript:CS}(l)} from Callaway & Sant'Anna (2020)") ///
								cols(1) size(vsmall)) ///
			graphregion(color(white)) name(es_af_used, replace)

*ACRES_IRR
tw	(rcap scaled_ub scaled_lb effect_estimated if estimator=="C&S" & dep_var=="acres_irr" & extreme!=1,  msize(.2) lwidth(.1) lcolor(black%70)) /// 
	(line scaled_estimate effect_estimated if estimator=="C&S" & dep_var=="acres_irr", lwidth(.1) lcolor(maroon%70)) ///
	(scatter scaled_estimate effect_estimated if estimator=="C&S" & dep_var=="acres_irr", msize(.2) mcolor(maroon%70) msymbol(O)) /// 
	(rcap scaled_ub scaled_lb effect_estimated if extreme==1 & dep_var=="acres_irr",  msize(0) lpattern("shortdash") lwidth(.1) lcolor(black%70)), /// 
			title("        Flood to center pivot", size(small)) ///
			subtitle("Acres irrigated", size(small)) ///
			xtitle("Time relative to adoption (years)", size(vsmall)) ///
			yline(0, lpattern(solid)) ///
			ytitle("Percent change", size(small)) ///
			ylab(-40(20)40, ang(45) nogrid) ///
			xlab(-8(2)8, nogrid) ///
			legend(order(2) label(2 "{it:{&delta}{superscript:CS}(l)} from Callaway & Sant'Anna (2020)") ///
								cols(1) size(vsmall)) ///
								graphregion(color(white) margin(zero)) name(es_acres_irr, replace)

*DEPTH APPLIED
tw	(rcap scaled_ub scaled_lb effect_estimated if estimator=="C&S" & dep_var=="depth_applied" & extreme!=1,  msize(.2) lwidth(.1) lcolor(black%70)) /// 
	(line scaled_estimate effect_estimated if estimator=="C&S" & dep_var=="depth_applied", lwidth(.1) lcolor(maroon%70)) ///
	(scatter scaled_estimate effect_estimated if estimator=="C&S" & dep_var=="depth_applied", msize(.2) mcolor(maroon%70) msymbol(O)) /// 
	(rcap scaled_ub scaled_lb effect_estimated if extreme==1 & dep_var=="depth_applied",  msize(0) lpattern("shortdash") lwidth(.1) lcolor(black%70)), /// 
			title("        Flood to center pivot", size(small)) ///
			subtitle("Depth applied", size(small)) ///
			xtitle("Time relative to adoption (years)", size(vsmall)) ///
			yline(0, lpattern(solid)) ///
			ytitle("Percent change", size(small)) ///
			ylab(-40(20)40, ang(45) nogrid) ///
			xlab(-8(2)8, nogrid) ///
			legend(order(2) label(2 "{it:{&delta}{superscript:CS}(l)} from Callaway & Sant'Anna (2020)") ///
								cols(1) size(vsmall)) ///
								graphregion(color(white) margin(zero)) name(es_depth_applied, replace)
			
/* Combine the graphs */
grc1leg2 es_af_used es_acres_irr es_depth_applied, ///
	rows(3) cols(1) legendfrom(es_af_used) ///
	maintotoptitle maintitlefrom(es_af_used) ///
	xtob1title xtitlefrom(es_af_used) ///
	ytol1title ytitlefrom(es_af_used) ///
	labsize(vsmall) graphregion(color(white)) xsize(6.5) ysize(8) iscale(.8) ring(1) noautosize
graph export "${dr_output_app}/figureD1.tif", width(6500) height(8000) replace


/***************************** CP to LEPA *******************************/			
/* Import the cp to lepa event study coefficients */
	import excel "${dr_temp}/cs_cptolepa_96_05.xlsx", firstrow sheet(cs_event) clear
	gen estimator = "C&S"
	foreach var of varlist effect_estimated estimate se_estimate c_crit_val panel_mean_dep_var {
		destring `var', replace
	}
	gen scaled_estimate = 100*estimate/panel_mean_dep_var
	rename (ci_95_lb ci_95_ub) (lb_estimate ub_estimate)
	gen scaled_lb = 100*lb_estimate/panel_mean_dep_var
	gen scaled_ub = 100*ub_estimate/panel_mean_dep_var
	keep dep_var estimator effect_estimated scaled_estimate scaled_lb scaled_ub estimate lb_estimate ub_estimate
		
	*Generate column indicating tech transition and save
	gen transition = "cp_to_lepa"
 
*Sort 
sort dep_var effect_estimated

*Replace extreme ub and lb estimates with -50 and +50 to make graphs readable 
gen extreme = scaled_ub > 40 | scaled_lb < -40
replace scaled_ub = 40 if scaled_ub > 40
replace scaled_lb = -40 if scaled_lb < -40

/* Graph C&S together with C&D */
*AF_USED_IRR
tw	(rcap scaled_ub scaled_lb effect_estimated if estimator=="C&S" & dep_var=="af_used" & extreme!=1,  msize(.2) lwidth(.1) lcolor(black%70)) /// 
	(line scaled_estimate effect_estimated if estimator=="C&S" & dep_var=="af_used", lwidth(.1) lcolor(maroon%70)) ///
	(scatter scaled_estimate effect_estimated if estimator=="C&S" & dep_var=="af_used", msize(.2) mcolor(maroon%70) msymbol(O)) /// 
	(rcap scaled_ub scaled_lb effect_estimated if extreme==1 & dep_var=="af_used",  msize(0) lpattern("shortdash") lwidth(.1) lcolor(black%70)), /// 
			title("        Traditional center pivot to LEPA (1996 to 2005)", size(small)) ///
			subtitle("Acre-feet withdrawn", size(small)) ///
			xtitle("Time relative to adoption (years)", size(vsmall)) ///
			yline(0, lpattern(solid)) ///
			ytitle("Percent change", size(small)) ///
			ylab(-20(10)20, ang(45) nogrid) ///
			xlab(-8(2)8, nogrid) ///
			legend(order(2) label(2 "{it:{&delta}{superscript:CS}(l)} from Callaway & Sant'Anna (2020)") ///
								cols(1) size(vsmall)) ///
			graphregion(color(white) margin(zero)) name(es_af_used, replace)

*ACRES_IRR
tw	(rcap scaled_ub scaled_lb effect_estimated if estimator=="C&S" & dep_var=="acres_irr" & extreme!=1,  msize(.2) lwidth(.1) lcolor(black%70)) /// 
	(line scaled_estimate effect_estimated if estimator=="C&S" & dep_var=="acres_irr", lwidth(.1) lcolor(maroon%70)) ///
	(scatter scaled_estimate effect_estimated if estimator=="C&S" & dep_var=="acres_irr", msize(.2) mcolor(maroon%70) msymbol(O)) /// 
	(rcap scaled_ub scaled_lb effect_estimated if extreme==1 & dep_var=="acres_irr",  msize(0) lpattern("shortdash") lwidth(.1) lcolor(black%70)), /// 
			title("        Center-pivot to LEPA", size(small)) ///
			subtitle("Acres irrigated", size(small)) ///
			xtitle("Time relative to adoption (years)", size(vsmall)) ///
			yline(0, lpattern(solid)) ///
			ytitle("Percent change", size(small)) ///
			ylab(-20(10)20, ang(45) nogrid) ///
			xlab(-8(2)8, nogrid) ///
			legend(order(2) label(2 "{it:{&delta}{superscript:CS}(l)} from Callaway & Sant'Anna (2020)") ///
								cols(1) size(vsmall)) ///
								graphregion(color(white) margin(zero)) name(es_acres_irr, replace)

*DEPTH APPLIED
tw	(rcap scaled_ub scaled_lb effect_estimated if estimator=="C&S" & dep_var=="depth_applied" & extreme!=1,  msize(.2) lwidth(.1) lcolor(black%70)) /// 
	(line scaled_estimate effect_estimated if estimator=="C&S" & dep_var=="depth_applied", lwidth(.1) lcolor(maroon%70)) ///
	(scatter scaled_estimate effect_estimated if estimator=="C&S" & dep_var=="depth_applied", msize(.2) mcolor(maroon%70) msymbol(O)) /// 
	(rcap scaled_ub scaled_lb effect_estimated if extreme==1 & dep_var=="depth_applied",  msize(0) lpattern("shortdash") lwidth(.1) lcolor(black%70)), /// 
			title("        Center-pivot to LEPA", size(small)) ///
			subtitle("Depth applied", size(small)) ///
			xtitle("Time relative to adoption (years)", size(vsmall)) ///
			yline(0, lpattern(solid)) ///
			ytitle("Percent change", size(small)) ///
			ylab(-20(10)20, ang(45) nogrid) ///
			xlab(-8(2)8, nogrid) ///
			legend(order(2) label(2 "{it:{&delta}{superscript:CS}(l)} from Callaway & Sant'Anna (2020)") ///
								cols(1) size(vsmall)) ///
								graphregion(color(white) margin(zero)) name(es_depth_applied, replace)
			
/* Combine the graphs */
grc1leg2 es_af_used es_acres_irr es_depth_applied, ///
	rows(3) cols(1) legendfrom(es_af_used) ///
	maintotoptitle maintitlefrom(es_af_used) ///
	xtob1title xtitlefrom(es_af_used) ///
	ytol1title ytitlefrom(es_af_used) ///
	labsize(vsmall) graphregion(color(white)) xsize(6.5) ysize(8) iscale(.8) ring(1) noautosize
graph export "${dr_output_app}\figureD2.tif", width(6500) height(8000) replace